<template>
  <div :class="themeClass">
    <div class="home">
      <div class="menu-item" v-for="(item, index) in list" :key="index">
        <!-- 子菜单 -->
        <transition name="fade" v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:after-enter="afterEnter" v-on:before-leave="beforeLeave" v-on:leave="leave" v-on:after-leave="afterLeave">
          <div class="sub-menu" v-show="item.childs.length && item.show">
          <!-- <div class="sub-menu"> -->
            <div class="sub-menu-item" v-for="(subItem, index1) in item.childs" :key="index1" :class="{ subClick: subItem.selected }" @click.stop="subClick(subItem, index1, index)">
              <div class="sub-title" :class="{ boxCenter: index == 1 }">
                <span class="iconImg" v-if="subItem.icon">
                  <img v-if="!subItem.selected" :src="subItem.icon" alt />
                  <img v-else :src="subItem.clickIcon" alt />
                </span>
                <div>{{ subItem.text }}</div>
              </div>
              <div class="third-menu" v-if="subItem.selected && subItem.childs.length">
                <div
                  class="third-menu-item"
                  :class="{ 'third-menu-item-click': thirdItem.selectedThird }"
                  v-for="(thirdItem, index2) in subItem.childs"
                  :key="index2"
                  @click.stop="thirdMenuClick(index, index1, index2, thirdItem)"
                >
                  {{ thirdItem.text }}
                </div>
              </div>
            </div>
            <div class="sub-title sub-close" v-if="item.childs.length" @click.stop="closeSub(index)">
              <img src="../../assets/images/arrow.png" alt />
            </div>
          </div>
        </transition>
        <!-- 父级菜单 -->
        <div class="menu-title" :class="{ selected: item.show }" @click.stop="selectedItem(item, index)">
          <span :class="{ spanActive: item.show }">{{ item.text }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  props: {
    list: {
      type: Array
    }
  },
  data () {
    return {
      theme1: 'dark'
    }
  },
  computed: {
    ...mapState(['theme']),
    themeClass () {
      return `theme-${this.theme1}`
    }
  },
  methods: {
    // 一级菜单
    selectedItem (item, index) {
      this.list[index].show = !this.list[index].show
      if (this.list[index].show) {
        this.list.forEach((item, index1) => {
          if (index1 !== index) {
            item.show = false
          }
        })
      }
      this.$emit('layerChange', item.tag, item.show)
    },
    // 二级
    subClick (item, index1, index) {
      item.selected = true
      console.log(item)
      this.list[index].childs.forEach((item, i) => {
        if (index1 !== i) {
          item.selected = false
        }
      })
      if (!item.childs || item.childs.length <= 0) {
        this.list[index].show = !this.list[index].show
      }
      if (item.target) {
        // window.location.href = item.target;
        window.open(item.target, '_blank')
      }
      this.$emit('layerChange', item.tag)
    },
    // 三级列表
    thirdMenuClick (firstIndex, secondIndex, thirdIndex, thirdItem) {
      this.list[firstIndex].childs[secondIndex].childs[thirdIndex].selectedThird = true
      this.list[firstIndex].childs[secondIndex].childs.forEach((item, i) => {
        if (i !== thirdIndex) {
          item.selectedThird = false
        }
      })
      if (!thirdItem.childs || thirdItem.childs.length <= 0) {
        this.list[firstIndex].show = !this.list[firstIndex].show
      }
      console.log('thirdItem.tag', thirdItem.tag)
      this.$emit('layerChange', thirdItem.tag)
    },
    closeSub (index) {
      this.list[index].show = false
    },
    beforeEnter (el) {
      el.className = el.className + ' my-transition'
      el.style.height = '53px'
    },
    enter (el) {
      el.style.height = el.scrollHeight + 'px'
    },
    afterEnter (el) {
      el.className = el.className.replace('my-transition', '')
      el.style.height = ''
    },
    beforeLeave (el) {
      el.style.height = el.scrollHeight + 'px'
    },
    leave (el) {
      let ht = el.scrollHeight
      el.className = el.className + ' my-transition'
      el.style.height = 0
    },
    afterLeave (el) {
      el.className = el.className.replace('my-transition', '')
      el.style.height = ''
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/index.scss';
.home {
  // height: 50px;
  // width: 875px;
  height: 5.5555vh;
  width: 45.6vw;
  display: flex;
  align-items: center;
  // background:chartreuse;
  @include themify($themes) {
    background-image: themed('nav-menu-bg');
    background-position-x: -0.625rem /* 10/16 */;
  }
  background-size: 100% 100%;
  z-index: 99;
  .menu-item:nth-of-type(1) {
    .sub-menu {
      left: 1.4vw;
    }
    .selected {
      background-position: -0.2vw -5.81vh;
    }
  }
  .menu-item:nth-of-type(2) {
    width: 8.8542vw;
    .sub-menu {
      left: 0.6vw;
    }
    .selected {
      background-position: -0.4vw -5.81vh;
    }
  }
  .menu-item:nth-of-type(3) {
    width: 8.5938vw;
    .selected {
      background-position: -0.4vw -5.81vh;
    }
  }
  .menu-item:nth-of-type(4) {
    width: 8.8542vw;
    .sub-menu {
      left: 0.6vw;
    }
    .selected {
      background-position: -0.4vw -5.81vh;
    }
  }
  .menu-item:nth-of-type(5) {
    .selected {
      background-position: -0.4vw -5.81vh;
    }
  }
}
.menu-item {
  // flex: 1;
  width: 9.1146vw;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  position: relative;
  cursor: pointer;
  .menu-title {
    position: relative;
    z-index: 99;
    height: 4.6vh;
    line-height: 4.6vh;
    font-size: 0.93vw;
    font-family: PingFang SC;
    font-weight: bold;
    cursor: pointer;
    @include themify($themes) {
      color: themed('nav-font-color');
    }
    > span {
      display: inline-block;
      height: 1.875rem /* 30/16 */;
      width: 7.5rem /* 120/16 */;
      line-height: 1.875rem /* 30/16 */;
      box-sizing: border-box;
      text-align: center;
    }
    > .spanActive {
      @include themify($themes) {
        @if (themed('nav-first-bg-select') != '') {
          // 黑色模式
          background: none;
        } @else {
          background: #0398ff;
          color: #fff;
        }
      }
    }
  }
  .selected {
    // background: url('~@/assets/images/select-light.png') ;
    @include themify($themes) {
      @if (themed('nav-first-bg-select') != '') {
        // 黑色模式下才要背景图
        background: themed('nav-first-bg-select');
        // background-position: 0.52vw -5.81vh;
      } @else {
        background: none;
      }
      color: themed('nav-font-color-select');
    }
    width: 100%;
  }
  .sub-menu {
    @include themify($themes) {
      border: themed('nav_border_color');
      box-shadow: themed('nav_border_box_shadow');
    }
    width: 6.95vw;
    position: absolute;
    bottom: 4.6666vh;
    left: 0.5208vw;
    z-index: -1;
    padding-top: 1vh;
    cursor: pointer;
    @include themify($themes) {
      background: themed('nav-first-bg');
    }

    .sub-title {
      width: 100%;
      height: 4.696vh;
      display: inline-block;
      @include themify($themes) {
        color: themed('nav-font-color');
      }
      font-weight: 600;
      text-align: center;
      font-size: 0.73vw;
      font-family: PingFang SC;
      font-weight: 600;
      display: flex;
      align-items: center;
      cursor: pointer;
      .iconImg {
        display: inline-block;
        width: 0.94vw;
        height: 0.94vw;
        margin-left: 0.5vw;
        margin-right: 0.5vw;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .boxCenter {
      div {
        width: 100%;
        text-align: center;
      }
    }
    .subClick {
      .sub-title {
        @include themify($themes) {
          background: themed('nav-second-bg-select');
          color: themed('nav-second-font-select') !important;
        }
      }
    }

    .sub-close {
      display: flex;
      justify-content: center;
    }
  }
}
.sub-menu-item {
  position: relative;
  @include themify($themes) {
    color: themed('$font-color-grey') !important;
  }
  .third-menu {
    position: absolute;
    top: 0;
    left: 6.98vw;
    min-width: 9.38vw;
    min-height: 3.43vh;
    @include themify($themes) {
      background: themed('nav-first-bg');
    }
    .third-menu-item {
      @include themify($themes) {
        box-shadow: themed('nav_box_shadow');
      }
      height: 3.7vh;
      font-size: 0.73vw;
      line-height: 3.7vh;
      font-family: PingFang SC;
      font-weight: 600;
      color: #7a88a4;
      text-align: center;

      @include themify($themes) {
        color: themed('nav-third-font');
      }
    }
    .third-menu-item-click {
      @include themify($themes) {
        background: themed('nav-third-bg-select');
        color: themed('nav-third-font-select');
        // background-size: 100% 100%;
      }
    }
  }
}
.sub-menu-item:hover {
  .sub-title {
    @include themify($themes) {
      background: themed('nav-second-bg-select');
      color: themed('nav-second-font-select') !important;
    }
  }
}
// 动画
.my-transition {
  transition: 0.5s height ease-in-out; /* , 10.5s padding-top ease-in-out, 10.5s padding-bottom ease-in-out */
}
</style>
